<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="商品名称:" prop="spuName">
        <el-input v-model="queryParams.spuName" size="small" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="货号:" prop="sn">
        <el-input v-model="queryParams.sn" size="small" placeholder="请输入货号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品分类:" prop="categoryId">
        <el-cascader v-model="queryParams.categoryOption" style="width: 100%" :options="options.categoryOptions" :props="{value: 'id', label: 'categoryName'}" placeholder="请选择商品分类" />
      </el-form-item>
      <el-form-item label="品牌:" prop="brandId">
        <el-select v-model="queryParams.brandId" size="small" filterable clearable placeholder="请输入选择品牌">
          <el-option v-for="option in options.brandOptions" :key="option.id" :label="option.brandName" :value="option.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:spu:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['goods:spu:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:spu:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:spu:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleLogicalDelete">作废</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:spu:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAddFromCloud">从云仓选商品添加</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <img style="height: 80px" :src="attachUrl(scope.row.image)">
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="spuName" >
      <template slot-scope="scope">
        <el-button type="text" @click="handleView(scope.row)">{{ scope.row.spuName }}</el-button>
      </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brandName" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <!--<el-table-column label="货号" align="center" prop="sn" />-->
      <el-table-column label="商品售价" align="center" prop="price" />
      <el-table-column label="商品库存" align="center" prop="stock" />
      <el-table-column label="预警库存" align="center" prop="warningStock" />
      <el-table-column label="销量" align="center" prop="saleQty" />
      <el-table-column label="上架" align="center" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isMarketable" active-value="0" inactive-value="1" @change="handleIsMarketableChange(scope.row)" />
        </template>
      </el-table-column>
      <!--<el-table-column label="状态" align="center" prop="status" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['goods:spu:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
     <!--     <el-button v-hasPermission="['goods:spu:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
     -->   </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />
  </div>
</template>

<script>
import SpuApi from '@/api/goods/SpuApi'
import BrandApi from '@/api/goods/BrandApi'
import CategoryApi from '@/api/goods/CategoryApi'

export default {
  name: 'PmsSpuList',
  data() {
    return {
      options: {
        brandOptions: [],
        categoryOptions: [],
        sysYnTypeOptions: []
      },
      queryParams: {
        spuName: null,
        sn: null,
        title: null,
        brandId: null,
        categoryId1: null,
        categoryId2: null,
        categoryId3: null,
        specParamTemplateId: null,
        freightTemplateId: null,
        specs: null,
        params: null,
        image: null,
        images: null,
        introduction: null,
        saleService: null,
        saleQty: null,
        commentQty: null,
        isMarketable: null,
        isEnableSpec: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      BrandApi.list().then(response => {
        this.options.brandOptions = response.data
      })
      CategoryApi.tree().then(response => {
        this.options.categoryOptions = response.data
      })
    },
    handleAddFromCloud() {
      this.$router.push('/spu/handleAddFromCloud')
    },
    handleAdd() {
      this.$router.push('/spu/form/')
    },
    handleView(row) {
      this.$router.push({ path: `/spu/skuView/${row.id}` })
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SpuApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleLogicalDelete(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认作废数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SpuApi.logicalDelete(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('作废成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.$router.push({ path: '/spu/form/' + id })
    },
    handleQuery() {
      this.loading = true
      SpuApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出所有SPU，标准产品单位（Standard Product Unit ），是商品信息聚合的最小单位，是一组可复用、易检索数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SpuApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    handleIsMarketableChange(row) {
      const display = row.isMarketable === '0' ? '上架' : '下架'
      this.$confirm('确认要"' + display + '"商品吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SpuApi.edit({ id: row.id, isMarketable: row.isMarketable })
      }).then(() => {
        this.$message.success(display + '成功')
        this.handleQuery()
      }).catch(function() {
        row.isMarketable = row.isMarketable === '0' ? '1' : '0'
      })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
